home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6052 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: solon.com!not-for-mail
  2. From: k-ohara@ux7.cso.uiuc.edu (Keith Edward O'hara)
  3. Newsgroups: comp.lang.c.moderated,comp.lang.c
  4. Subject: Re: HELP IN WRITING MY FIRST PROGRAM ASSINGMENT
  5. Date: 22 Feb 1996 06:31:40 -0600
  6. Organization: University of Illinois at Urbana Champaign CCSO
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4ghnnc$dj9@solutions.solon.com>
  10. References: <3127FF7A.6442C3B8@eden.com> <4gfhkj$3p8@solutions.solon.com> <4ggbi9$83k@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12.  
  13. Previously in comp.lang.c.moderated,
  14. >int
  15. >cmp(void *a, void *b) {
  16. >        if (a < b) return -1;
  17. >                if (a > b) return 1;
  18. >        else return 0;
  19. >}
  20. was used as the callback function for qsort() (in an evil piece of code).
  21.  
  22. Soon thereafter, mcv@pi.net (Miguel Carrasquer Vidal) wrote:
  23. > I tried it (using Borland C 4.5 large model), and it didn't work.  
  24.  ...
  25. >I wonder what my qsort() is doing wrong?
  26.  
  27. So I had to try it.  The qsort in Borland's library, and the one I wrote
  28. when I was learning C, both call the callback compare function with one
  29. address OUTSIDE the original array.  In my qsort, I put the partitioning
  30. element in temporary storage (which I need anyway to do the swaps) and
  31. pass the address of the temporary storage as one argument to cmp().
  32.  
  33. >[but the whole point was that, sorting an array by address is legal, but
  34. > does nothing.  cmp was *intentionally* lying through its teeth, so
  35. > qsort would be a noop.  swap(), as correctly pointed out, is never
  36. > called... It was in there because I was going to use my own quicksort,
  37. > before I realized the stdlib one would work. -mod]
  38.  
  39. Two questions follow:
  40. Did Peter Seebach know all along that the pointers passed to cmp() would be
  41. not in the same array on some systems? 
  42. [No.  -mod]
  43. Does the standard (which I don't have to own since I'm not posting to
  44. comp.std.c) disallow this behavior of qsort()?
  45. [I hope so.]
  46.  
  47. keith
  48. (who really shouln't be spending time on `evil' code,
  49. but just can't help it.)
  50.